package com.example.fengyungo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fengyungo.R;
import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.domain.ChatMessage;
import com.example.fengyungo.utils.BitmapUtils;

import java.util.List;

/**
 * 聊天界面的recyclerview所用的适配器
 * @author XLR
 */
public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.ViewHolder> {

    private static final String TAG = "ChatMessageAdapter";

    private List<ChatMessage> chatMessageList;

    protected Context mContext;

    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftText;
        TextView rightText;
        ImageView leftAvatar;
        ImageView rightAvatar;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            leftLayout = itemView.findViewById(R.id.layout_left);
            rightLayout = itemView.findViewById(R.id.layout_right);
            leftText = itemView.findViewById(R.id.text_left);
            rightText = itemView.findViewById(R.id.text_right);
            leftAvatar = itemView.findViewById(R.id.avatar_left);
            rightAvatar = itemView.findViewById(R.id.avatar_right);
        }
    }

    public ChatMessageAdapter(List<ChatMessage> chatMessageList) {
        this.chatMessageList = chatMessageList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ChatMessage chatMessage = chatMessageList.get(position);
        // 头像设置
        if (chatMessage.getMessageType() == ChatMessage.FROM_RECEIVER) {
            // 来自接收者
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftText.setText(chatMessage.getContent());
            // 设置头像
            Bitmap bitmapFromSp = BitmapUtils.getBitmapFromSharedPreferences((FragmentActivity) mContext, chatMessage.getUserId());
            BitmapDrawable avatarDrawable = new BitmapDrawable(mContext.getResources(), bitmapFromSp);
            holder.leftAvatar.setBackground(avatarDrawable);
            Log.d(TAG, "onBindViewHolder: leftAvatar:"+avatarDrawable);
        } else if (chatMessage.getMessageType() == ChatMessage.FROM_SENDER) {
            // 来自发送者
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightText.setText(chatMessage.getContent());
            // 设置头像-司机自己
            Bitmap bitmapFromSp = BitmapUtils.getBitmapFromSharedPreferences((FragmentActivity) mContext, DriverConstant.AVATAR);
            BitmapDrawable avatarDrawable = new BitmapDrawable(mContext.getResources(), bitmapFromSp);
            holder.rightAvatar.setBackground(avatarDrawable);
            Log.d(TAG, "onBindViewHolder: rightAvatar:"+avatarDrawable);
        }
    }

    @Override
    public int getItemCount() {
        return chatMessageList.size();
    }
}
